<template>
  <div class="app-container">
    <el-row>
  <el-col :span="5"><div class="grid-content bg-purple">
    <el-tree
  :data="tree_data"
  node-key="id"
  default-expand-all
  @node-drag-start="handleDragStart"
  @node-drag-enter="handleDragEnter"
  @node-drag-leave="handleDragLeave"
  @node-drag-over="handleDragOver"
  @node-drag-end="handleDragEnd"
  @node-drop="handleDrop"
  draggable
  :allow-drop="allowDrop"
  :allow-drag="allowDrag"
   @node-click="handleNodeClick"
  >
</el-tree>
    </div></el-col>
  <el-col :span="19"><div class="grid-content bg-purple-light">
       <el-button
      size="mini"
      type="primary"
      v-hasPermi="['admin:table:table:post']"
      @click="on_add"
      icon="el-icon-plus"
      >添加表</el-button
    >
    <div
      style="
        border: 1px #dcdfe6 solid;
        margin: 10px 0px;
        width: 100%;
        overflow-x: auto;
      "
    >
      <!-- <el-table :data="data" border stripe size="medium" @filter-change="on_change_filter" @sort-change="on_change_sort" v-loading="loading" style="width: 100%"> -->
      <el-table :data="data" border stripe size="medium" style="width: 100%">
        <el-table-column label="表名" prop="name" :show-overflow-tooltip="true" disabled="true">
        </el-table-column>
        <el-table-column
          label="别名"
          prop="alias"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="描述"
          prop="readme"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="创建人"
          prop="creator_username"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="creation_time"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="text"
                  plain
                  icon="el-icon-s-order"
                  size="mini"
                  @click="handleDetail(scope.row)"
                  v-hasPermi="['admin:table:table:get']"
                  >详情</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="text"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd(scope.row)"
                  v-hasPermi="['admin:table:field:get']"
                  >字段</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="text"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['admin:table:table:{id}:put']"
                  >修改</el-button
                >
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="text"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['admin:table:table:{id}:delete']"
                  >删除</el-button
                >
              </el-col>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      style="float: right; margin-right: 100px"
      background
      layout="total, prev, pager, next"
      :page-size="page_size"
      :total="total_num"
      :current-page.sync="current_page"
      @current-change="on_page_change"
    >
    </el-pagination>

    <!-- 新增数据表 -->
    <el-dialog
      width="900px"
      :title="chang_or_add ? '修改表 ' + form_data.name : '创建表'"
      :visible.sync="open"
      :before-close="on_close"
    >
      <el-form size="small" label-width="60px" style="margin-right: 30px">
        <el-row>
                              <el-col :span="8">
            <el-form-item
              label="分类:"
              :error="String(errors.alias ? errors.alias : '')"
            >
<div class="block">
  <el-cascader
    v-model="optiontype"
    :options="options"
    :key="isResouceShow"
    :props="{ checkStrictly: true }"
    :show-all-levels="false"
    clearable></el-cascader>
</div>            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="表名:"
              :error="String(errors.name ? errors.name : '')"
            >
              <el-input :disabled="chang_or_add" v-model="form_data.name">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="别名:"
              :error="String(errors.alias ? errors.alias : '')"
            >
              <el-input v-model="form_data.alias"></el-input>
            </el-form-item>
          </el-col>

        </el-row>
        <el-form-item
          label="描述:"
          :error="String(errors.readme ? errors.readme : '')"
        >
          <el-input v-model="form_data.readme" type="textarea"></el-input>
        </el-form-item>
      </el-form>

      <el-form label-width="60px" style="margin-right: 30px">
        <el-form-item>
          <el-button
            size="small"
            style="width: 100%"
            type="primary"
            :loading="loading"
            @click="on_submit()"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增字段 -->
    <fieldAdd @listenChileEvent="getList"  ref="childfieldAdd"></fieldAdd>
    <fieldDetail @listenChileEvent="getList"  ref="childfieldDetail"></fieldDetail>
    </div></el-col>
</el-row>
 

  </div>
</template>

<script>
import fieldAdd from './components/Field'
import fieldDetail from './components/Detail'

// import ShowTable from './components/ShowTable'
// import ChangeTable from './components/ChangeTable'
// import DeleteTable from './components/DeleteTable'

import { addTable, listTable, delTable,updateTable,listTree } from "@/api/vadmin/cmdb/table";
export default {
  name: "Tablemgt",
  components: {
    fieldAdd,
    fieldDetail
    // ShowTable,
    // ChangeTable,
    // DeleteTable
  },
  data: () => {
    return {
      data: [],
      query: "",
      tables: [],
      loading: false,
      current_query: "*",
      indices: [],
      current_indices: [],
      page: 1,
      page_size: 10,
      last_source: null,
      search_input_icon: "el-icon-edit",
      page_num: 0,
      current_page: 1,
      total_num: 0,
      all_history: {},
      username: "",
      history: [],
      open: false,
      options:[],
      isResouceShow: 0,
      optiontype:[0],
      form_data: { name: "", alias: "", readme: "" },
      errors: {},
      chang_or_add: false,
              tree_data: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
    };
  },
  created() {
    this.getList();
    this.getTree();
  },

  methods: {
    getTree(){
      listTree().then((response) => {
        this.tree_data=response
        console.log(">>>>>>>>>>>>>>>>>>>",this.tree_data)
        this.options=response
      })
    },
    handleNodeClick(val){
      console.log(val)
    },
          handleDragStart(node, ev) {
        console.log('drag start', node);
      },
      handleDragEnter(draggingNode, dropNode, ev) {
        console.log('tree drag enter: ', dropNode.label);
      },
      handleDragLeave(draggingNode, dropNode, ev) {
        console.log('tree drag leave: ', dropNode.label);
      },
      handleDragOver(draggingNode, dropNode, ev) {
        console.log('tree drag over: ', dropNode.label);
      },
      handleDragEnd(draggingNode, dropNode, dropType, ev) {
        console.log(this.tree_data)
        console.log('tree drag end: ', dropNode && dropNode.label, dropType);
      },
      handleDrop(draggingNode, dropNode, dropType, ev) {
        console.log('tree drop: ', dropNode.label, dropType);
      },
      allowDrop(draggingNode, dropNode, type) {
        if (dropNode.data.label === '二级 3-1') {
          return type !== 'inner';
        } else {
          return true;
        }
      },
      allowDrag(draggingNode) {
        return draggingNode.data.label.indexOf('三级 3-2-2') === -1;
      },
    on_page_change(val) {
      this.current_page = val;
      console.log(val);
      this.getList({ page: val });
    },
    on_add() {
      this.optiontype=[0]
      this.open = true;
      this.chang_or_add=false;
      this.form_data={};
    },
    on_close() {
      this.open = false;
    },
    on_mod_table(table) {
      // var vm = this.$refs.childAddTable.on_ADD(true,table);
    },
    on_show_detail(table) {
      // var vm = this.$refs.childShowTable.on_show(true,table);
    },
    on_change_fields(table) {
      this.$store.state.cmdb.item = table;
      // var vm = this.$refs.childChangeTable.on_show(true,table);
    },
    on_delete(table) {
      // var vm = this.$refs.childDeleteTable.on_show(true,table);
    },
    on_test(val) {
      console.log(val);
    },
    on_submit() {
      if (this.chang_or_add ==false){
        this.form_data["pid"]=this.optiontype[this.optiontype.length -1 ]
        addTable(this.form_data).then((response) => {
          this.open = false;
          this.getList();
          
          this.getTree();
        });
      }else{
        updateTable(this.form_data["id"],this.form_data).then((response)=>{
          this.msgSuccess("修改成功");
          this.getList();
          ++this.isResouceShow;
          this.getTree();
          this.open=false;
      })
      }

    },
    getList(query) {
      listTable(query).then((response) => {
        this.data = response.results;
        this.total_num = response.count;
      });
    },
    handleDetail(data) {
        this.$refs.childfieldDetail.on_show(true,data,false);
    },
     // 增加字段
    handleAdd(data) {
      this.$refs.childfieldAdd.on_show(true,data,false);
    },
    handleDelete(data) {
      this.$confirm('是否确认删除表"' + data.name + '"', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delTable(data.id);
        })
        .then(() => {
          this.getList({ page: this.current_page });
          this.msgSuccess("删除成功");
        })
        .catch((err) => {});
    },
    handleUpdate(data) {
      console.log("ddddddddddd",data)
      this.open=true;
      this.form_data=data;
      this.optiontype=data["pid"];
      this.chang_or_add=true;
    },
    handleExport() {},
  },
};
</script>
<style scoped>
.c-option-bar >>> .el-input-group__append {
  border-left: 0;
  background-color: #62a5f3 !important;
  color: #ffffff !important;
}

.el-radio {
  color: #606266;
  cursor: pointer;
  visibility: hidden; 
  margin-right: 30px;
}
</style>